
/*
*Pool Public Licence version 0.1 or later
*Released on 20th April 2008.
*Any voilations can be repoted at paritosh@wikiocean.net
*What is treated as voilations can be found at www.wikiocean.net/ppl1-voilations* 
******************************************************************************
* The contents of this file are subject Affero GPL v3 and later with additional conditions.
* The additional conditions are to be considered as part of the Affero GPL v3 and later.
* The additional conditions will supersede the Affero GPLv3 and later in case of conflict.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the License at http://www.gnu.org/licenses/agpl.html
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya.
* created by Paritosh Pungaliya are Copyright (C) Paritosh Pungaliya; All Rights Reserved.
* Contributions are Copyright (C) 2001-2008 Paritosh Pungaliya.

* Objective of the additional terms (license) 
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencie(s).
* 3) To avoid a situation where big corporates or investors can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the openness changes in companies that get Venture Capital funding.)
* (if you agree to this logic I invite you to our group to work together)

* Additional conditions
*  0) Source code will also include
* 	a) data dictionaries, data ontologies which are used to alter the behavior of code or to control           the features of application.
* 	b) Any code generated or derived automatically or manually from this code or logic
*  1) The revenue generated by deploying, implementing , selling services and other activities
*  	 based on this source code should be shared 100%, between the people working on it.
*  2) Capital invested should be given only fixed rate of return or linked to revenue/surplus growth for limited time.
*  3) People working on the project should always have an option to replace the capital provider in full or part with lower cost capital.
*  4) Their should not be any ownership which can be sold as a claim on future profit to be generated from the work of people working on the code.
*  5) Brand thus generated should belong to the group.
*  6) For old and new members revenue sharing should be equal for same work equivalent.
*  7) The share of revenue should be decided by bidding for work before allocation of work within the group.
*  8) Before bidding various method of measuring the work should be clearly defined and published in public domain. Any change in process should also be publised in public domain imediately.
*  9) All data related to to revenue sharing should be published as and when generated in public domain.
*  10) For group of people having size more than 10 people will have to make it open online for others to participate in bids. 
*  11) All people involved in related and sub project will be treated as a group.
*  12) Once this license is being used for a certain code then all and any other software services being provided by the group should complusorilly come under 100% revenue sharing.
*  13) Cross subsidization should not happen under this revenue share, no additional benefit should be drawn from the people, by the employing agency.
*  14) Any position or knowledge or legal structure created relating to people working on code, which adversely affects the spirit of 100% revenue share will be treated as a violation.
*  15) Revenue sharing defined.
*     a) includes direct revenue and also intangible gains like brand, and organization.
*     b) No personal judgment should be involved in judging the distribution of revenue.It has to be pre-defined formula based.
*     c)100% of revenue should be shared before expenses and recievers should have control over what expenses to incur.
*     d)For the work done in any month by contributing group member, working on any project can be given revenue share from the work done by the group, for a maximum of 18 months after that month.
*     e)Revenue should be apportioned to the month in which work is done and not when received.
*  The group fulfilling the above conditions also has to be a wiki ecosystem further defined in www.wekosystem.org or read about Wekosystem at www.wikiocean.net

*  ******************************************************************************
*/  
package combinereport.query;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
import java.sql.ResultSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;

import property.Order;
import file.SearchRecord;
import utility.CapitalChar;
import utility.ConvertToLower;
import utility.Index;
import utility.Input;
import database.RecordSearch;
import dbmanager.Column;
import dbmanager.DBManager;
import dbmanager.GetIdName;

/**
 * <pre>Query can contain a Query within it. It may be on same table or different one, 
 * but the actual mapping will be done at Join only.
 * As compare to SQL the Query  is mapped with Select Clause in SQL.</pre>
 * @author mahendra
 * 
 */
public class Query  implements Queryinterface 
{	
	String [] Querybuffer;
	DBManager database;
	String Query="SELECT DICTINCT * FROM";
	GetIdName gid;
	String tablename="report_master";
	String object;
	String[] buffer;
	int Querycount=1;
	static int QueryMaxcount=1;
	String QUERY="query";
	Hashtable<Object, Object> QueryMap = new Hashtable<Object, Object>();
	SearchRecord sr;
	Order order ;
	String 
/*
*Pool Public Licence version 0.1 or later
*Released on 20th April 2008.
*Any voilations can be repoted at paritosh@wikiocean.net
*What is treated as voilations can be found at www.wikiocean.net/ppl1-voilations* 
******************************************************************************
* The contents of this file are subject Affero GPL v3 and later with additional conditions.
* The additional conditions are to be considered as part of the Affero GPL v3 and later.
* The additional conditions will supersede the Affero GPLv3 and later in case of conflict.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the License at http://www.gnu.org/licenses/agpl.html
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya.
* created by Paritosh Pungaliya are Copyright (C) Paritosh Pungaliya; All Rights Reserved.
* Contributions are Copyright (C) 2001-2008 Paritosh Pungaliya.

* Objective of the additional terms (license) 
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencie(s).
* 3) To avoid a situation where big corporates or investors can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the openness changes in companies that get Venture Capital funding.)
* (if you agree to this logic I invite you to our group to work together)

* Additional conditions
*  0) Source code will also include
* 	a) data dictionaries, data ontologies which are used to alter the behavior of code or to control           the features of application.
* 	b) Any code generated or derived automatically or manually from this code or logic
*  1) The revenue generated by deploying, implementing , selling services and other activities
*  	 based on this source code should be shared 100%, between the people working on it.
*  2) Capital invested should be given only fixed rate of return or linked to revenue/surplus growth for limited time.
*  3) People working on the project should always have an option to replace the capital provider in full or part with lower cost capital.
*  4) Their should not be any ownership which can be sold as a claim on future profit to be generated from the work of people working on the code.
*  5) Brand thus generated should belong to the group.
*  6) For old and new members revenue sharing should be equal for same work equivalent.
*  7) The share of revenue should be decided by bidding for work before allocation of work within the group.
*  8) Before bidding various method of measuring the work should be clearly defined and published in public domain. Any change in process should also be publised in public domain imediately.
*  9) All data related to to revenue sharing should be published as and when generated in public domain.
*  10) For group of people having size more than 10 people will have to make it open online for others to participate in bids. 
*  11) All people involved in related and sub project will be treated as a group.
*  12) Once this license is being used for a certain code then all and any other software services being provided by the group should complusorilly come under 100% revenue sharing.
*  13) Cross subsidization should not happen under this revenue share, no additional benefit should be drawn from the people, by the employing agency.
*  14) Any position or knowledge or legal structure created relating to people working on code, which adversely affects the spirit of 100% revenue share will be treated as a violation.
*  15) Revenue sharing defined.
*     a) includes direct revenue and also intangible gains like brand, and organization.
*     b) No personal judgment should be involved in judging the distribution of revenue.It has to be pre-defined formula based.
*     c)100% of revenue should be shared before expenses and recievers should have control over what expenses to incur.
*     d)For the work done in any month by contributing group member, working on any project can be given revenue share from the work done by the group, for a maximum of 18 months after that month.
*     e)Revenue should be apportioned to the month in which work is done and not when received.
*  The group fulfilling the above conditions also has to be a wiki ecosystem further defined in www.wekosystem.org or read about Wekosystem at www.wikiocean.net

*  ******************************************************************************
*/  
packagename ="combinereport.query.";
	String Key="no$";
	String value = "no$";
	String QueryName;
	String Filename;
	String tid;
	String process;
	String ReportName;
	String typeofreport;
	Hashtable<Object, Object> table = new Hashtable<Object, Object>();
	Hashtable<Object, Object> result =new Hashtable<Object, Object>();
	Hashtable<Object, Object> Reportmap =new Hashtable<Object, Object>();
	Queryinterface query;
	String reporton;
	/**
	 * set Dbmanager object
	 * @param db :  Dbmanager object
	 */
	
	
	public void setDbmanager(DBManager database)
	{
		this.database = database;
		gid = new GetIdName(database);
		QUERY =  gid.getId(QUERY);
		order = new Order(database);
	}
	/**
	 * set Query Object 
	 * @param Object : String
	 */
	public void setObject(String object)
	{
		this.object = object;
	}
	/**
	 * this method set Querybuffer to result of inner query  
	 * @param Querybuffer
	 *   
	 */
	public void setQuerybuffer(String Querybuffer[])
	{
		this.buffer = Querybuffer;
	}
	/**
	 * initializeData()  method find object of query,condition,field,key,sortby,tablename and put into querymap
	 */
	public void initializeData()
	{
		result.put("tablename", "report_master");
		
		try 
		{
			
			Hashtable<Object, Object> temp = new Hashtable<Object, Object>();
			//String MasterPath = Input.MASTERREPORTSPATH + object + "//" + object + "_mid" + Input.FILRFORMAT;
			//sr = new SearchRecord(MasterPath);
			RecordSearch rs =new RecordSearch(database);
			temp.put("mid",object);
			temp.put("td", "null");
			rs.setConditionMap(temp);
			String line[] = rs.getArrayFromResultSet();
			QueryMap.clear();
//			setting default table name to report_master
			QueryMap.put(gid.getId("tablename"), "report_master");
			if(typeofreport.equalsIgnoreCase("systemgenerate") && Querycount==1)
				QueryMap.put(gid.getId("condition"), "");
			for(int i=0;i<line.length;i++)
			{
				String splitstr[]  = line[i].split("\t");
				String value = splitstr[Column.pv_index-1];
				if(value.equals("0"))
					value = splitstr[Column.vt_index-1];
				else
					value = gid.getItem(splitstr[Column.pv_index-1]);
				QueryMap.put((splitstr[Column.pid_index-1]) , value);
			}
			this.QueryMap = ConvertToLower.convertHashKey(QueryMap);			
			//String MASTERPATH = Input.MASTERREPORTSPATH + object + "//" + object + "_mid" + Input.FILRFORMAT;
			//sr = new SearchRecord(MASTERPATH);

			table.put("mid", object);
			rs.setConditionMap(temp);
			line = rs.getArrayFromResultSet();
			result.putAll(gethashtabelofResult(line));
		} catch (Exception e1) {
			e1.printStackTrace();
			result.putAll(QueryMap);
		}		
		
		if(typeofreport.equalsIgnoreCase("systemgenerate"))
			result.put(("condition"), "");
		
	//	result = gid.ConvertKeytoMasterName(result);
		table = order.getSequences(QUERY);
		table = ConvertToLower.convertHashKey(table);
		result = ConvertToLower.convertHashKey(result); 
		
		for(int i=0;i<table.size();i++)
		{
			try 
			{
				String classname = table.get(Integer.toString(i+1)).toString();
				System.out.println("Class Name is "+classname);				
				String tempclassname = gid.getId(classname);			
				if(QueryMap.containsKey(tempclassname))
				{
					Class c =Class.forName(
/*
*Pool Public Licence version 0.1 or later
*Released on 20th April 2008.
*Any voilations can be repoted at paritosh@wikiocean.net
*What is treated as voilations can be found at www.wikiocean.net/ppl1-voilations* 
******************************************************************************
* The contents of this file are subject Affero GPL v3 and later with additional conditions.
* The additional conditions are to be considered as part of the Affero GPL v3 and later.
* The additional conditions will supersede the Affero GPLv3 and later in case of conflict.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the License at http://www.gnu.org/licenses/agpl.html
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya.
* created by Paritosh Pungaliya are Copyright (C) Paritosh Pungaliya; All Rights Reserved.
* Contributions are Copyright (C) 2001-2008 Paritosh Pungaliya.

* Objective of the additional terms (license) 
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencie(s).
* 3) To avoid a situation where big corporates or investors can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the openness changes in companies that get Venture Capital funding.)
* (if you agree to this logic I invite you to our group to work together)

* Additional conditions
*  0) Source code will also include
* 	a) data dictionaries, data ontologies which are used to alter the behavior of code or to control           the features of application.
* 	b) Any code generated or derived automatically or manually from this code or logic
*  1) The revenue generated by deploying, implementing , selling services and other activities
*  	 based on this source code should be shared 100%, between the people working on it.
*  2) Capital invested should be given only fixed rate of return or linked to revenue/surplus growth for limited time.
*  3) People working on the project should always have an option to replace the capital provider in full or part with lower cost capital.
*  4) Their should not be any ownership which can be sold as a claim on future profit to be generated from the work of people working on the code.
*  5) Brand thus generated should belong to the group.
*  6) For old and new members revenue sharing should be equal for same work equivalent.
*  7) The share of revenue should be decided by bidding for work before allocation of work within the group.
*  8) Before bidding various method of measuring the work should be clearly defined and published in public domain. Any change in process should also be publised in public domain imediately.
*  9) All data related to to revenue sharing should be published as and when generated in public domain.
*  10) For group of people having size more than 10 people will have to make it open online for others to participate in bids. 
*  11) All people involved in related and sub project will be treated as a group.
*  12) Once this license is being used for a certain code then all and any other software services being provided by the group should complusorilly come under 100% revenue sharing.
*  13) Cross subsidization should not happen under this revenue share, no additional benefit should be drawn from the people, by the employing agency.
*  14) Any position or knowledge or legal structure created relating to people working on code, which adversely affects the spirit of 100% revenue share will be treated as a violation.
*  15) Revenue sharing defined.
*     a) includes direct revenue and also intangible gains like brand, and organization.
*     b) No personal judgment should be involved in judging the distribution of revenue.It has to be pre-defined formula based.
*     c)100% of revenue should be shared before expenses and recievers should have control over what expenses to incur.
*     d)For the work done in any month by contributing group member, working on any project can be given revenue share from the work done by the group, for a maximum of 18 months after that month.
*     e)Revenue should be apportioned to the month in which work is done and not when received.
*  The group fulfilling the above conditions also has to be a wiki ecosystem further defined in www.wekosystem.org or read about Wekosystem at www.wikiocean.net

*  ******************************************************************************
*/  
packagename+CapitalChar.makeFirstCharCapital(classname));
					query = (Queryinterface) c.newInstance();
					query.setDbmanager(database);
					query.setObject(result.get(table.get(Integer.toString(i+1)).toString().toLowerCase()).toString().toLowerCase());
					query.settablename(tablename);
					tablename=query.gettablename();
					query.setbuffer(buffer);
					query.setQuerybuffer(Querybuffer);
					query.setKey(Key);
					Key = query.getkey();
					query.setValue(value);
					value = query.getvalue();
					query.setReportName(ReportName);
					query.setQueryMap(QueryMap);
					query.settid(tid);
					query.setCount(Querycount);
					query.setMaxcount(Querycount);
					Querycount=query.getcount();
					QueryMap.put(tempclassname, query);
				}
			}
			catch (Exception e) 
			{
				e.printStackTrace();
			}
		}	
	}
	
	public String getQueryName()
	{
		return QueryMap.get("query").toString();
	}
	public void setQueryName()
	{
		
	}
	
	/**
	 * This method evaluate query and put result into buffer
	 */
	public void getResult()
	{		
		for(int i=0;i<table.size();i++)
		{
			try 
			{
				String classname = table.get(Integer.toString(i+1)).toString();
				System.out.println("Class Name is "+classname);
				classname = gid.getId(classname).toLowerCase();
				if(QueryMap.containsKey(classname))
				{	
					query = (Queryinterface) QueryMap.get(classname);
					query.initializeData();
					query.getResult();
					buffer = query.getbuffer();
					Querybuffer = query.getQuerybuffer();
					tid= query.gettid();
					Filename = query.getReportname();
				}
			}
			catch (Exception e) 
			{
				e.printStackTrace();
			}
		}
	}
	/**
	 * set buffer  
	 * @param buffer : String array 
	 */
	public void setbuffer(String[] buffer) 
	{
		this.buffer = buffer;
	}
	/**
 	 * this method convert string array into hashtable 
	 * @param line ; String array
	 * @return hashtable : Hashtable 
	 */
	
	public Hashtable<Object, Object> gethashtabelofResult(String line[])
	{
		Hashtable<Object, Object> result = new Hashtable<Object, Object>();
		for(int i=0;i<line.length;i++)
		{	
			String splitline[] = line[i].split("\t");
			String value = splitline[Column.pv_index-1];
			if(value.equals("0"))
				value = splitline[Column.vt_index-1];		
			result.put(gid.getItem(splitline[Column.pid_index-1]), value);			
		}
		result=ConvertToLower.convertHashKey(result);
		return result;
	}

	public void generateReport()
	{	
			try 
			{
				File file = new File(Filename);
				if(!file.exists())
					file.createNewFile();
				RandomAccessFile raf = new RandomAccessFile(file,"rws");
				for(int i=0;i<buffer.length;i++)
				{
					raf.writeBytes(buffer[i]+"\n");
				}
			}
			catch (FileNotFoundException e) 
			{
				e.printStackTrace();
			} 
			catch (Exception e) 
			{
				e.printStackTrace();
			}
		
	}
	/**
	 * retrun buffer 
	 * @return buffer: String array  
	 */
	public String[] getbuffer()
	{
		return buffer;
	}
	/**
	 * return tablename 
	 * @return tablename:String  
	 * 
	 * 	 */
	public String gettablename() 
	{
		return tablename;
	}
	/**
	 * settablename() method takes following parameters
	 * @param tablename : table name of query
	 * settablename method set tablename of Condition class
	*/
	public void settablename(String tablename) 
	{ 
		this.tablename = tablename;
	}
	
	public static void main(String[] args) 
	{
		DBManager database = new DBManager();
		database.getConnect();
		Query query = new Query();
		query.setDbmanager(database);
		///query.setObject("6271");
		//query.setObject("6280");
		//query.setObject("6296");  //systemgenerate report for transaction
		//query.setObject("6347");   //Retailer upper report Report
		//query.setObject("6396"); //middle td report
		query.setObject("7296");//middlie full report
		//query.setObject("6435");
		//query.setObject("6254");
		query.setValue("4");
		query.settypeofreport("Systemgenerate");
		query.initializeData();
		query.updateReport();
		String buffer[] =query.getbuffer();
		for(int i=0;i<buffer.length;i++)
		{
			System.out.println(buffer[i]);
		}
	}


	public String[] getQuerybuffer() 
	{
		return buffer;
	}
	/**
	 * set key
	 * @param key :String	  
	 */
	public void setKey(String key) 
	{
		this.Key = "no$";
	}

	public void setValue(String value) 
	{
		this.value = value;
	}
	/**
	 * return key   
	 * @return key:String 
	 */
	public String getkey() 
	{
		return Key;
	}

	public String getvalue() 
	{
		return value;
	}

	public void setQueryName(String QueryName) 
	{
		this.QueryName = QueryName;
	}


	public String getReportname() 
	{
		return Filename;
	}


	public void setReportname(String Filename) 
	{
		this.ReportName = Filename;
	}

	
	public Hashtable<Object, Object> getQueryMap() 
	{
		return QueryMap;
	}


	
	/**
	 * return tid which is present in report_master 
	 * @return tid
	 */
	public String gettid() 
	{
		return tid;
	}
	/**
	 * set tid  
	 * @param tid :String 
	*/
	public void settid(String tid) 
	{
		this.tid =tid;	
	}

	/**
	 * this method evalute query and put result into buffer    
	 */
	public void addReport() 
	{
		for(int i=0;i<table.size();i++)
		{
			try 
			{
				String classname = table.get(Integer.toString(i+1)).toString();
				System.out.println("Class Name is "+classname);
				classname = gid.getId(classname).toLowerCase();
				if(QueryMap.containsKey(classname))
				{	
					query = (Queryinterface) QueryMap.get(classname);
					query.setbuffer(buffer);
					query.setQuerybuffer(Querybuffer);
					query.settypeofreport(typeofreport);
					query.setReportmap(Reportmap);
					query.settablename(tablename);
					query.setreporton(reporton);
					query.initializeData();
					query.addReport();
					buffer = query.getbuffer();
					Querybuffer = query.getQuerybuffer();		
					value = query.getvalue();
					tablename=query.gettablename();
					tid= query.gettid();
					Filename = query.getReportname();
				}
			}
			catch (Exception e) 
			{
				e.printStackTrace();
			}
		}
	}

	/**
	 * this method return buffer
	 * @return buffer :String array
	 */
	public String[] getBuffer() 
	{
		return buffer;
	}


	public void setProcess(String process)
	{
		this.process = process;
	}


	public String getReportName() 
	{
		return ReportName;
	}


	public void setBuffer(String[] buffer) 
	{	
		this.buffer = buffer;
	}


	public void setReportName(String ReportName) 
	{
		this.ReportName = ReportName;	
	}
	/**
	 * when process is mod then updateReport is call  
	 */
	public void updateReport()
	{	
		for(int i=0;i<table.size();i++)
		{
			try 
			{
				String classname = table.get(Integer.toString(i+1)).toString();
				System.out.println("Class Name is "+classname);
				classname = gid.getId(classname).toLowerCase();
				if(QueryMap.containsKey(classname))
				{	
					query = (Queryinterface) QueryMap.get(classname);
					query.setbuffer(buffer);
					query.setQuerybuffer(Querybuffer);
					query.settablename(tablename);
					query.setReportmap(Reportmap);
					query.settypeofreport(typeofreport);
					query.setreporton(reporton);					
					query.initializeData();		
					query.setValue(value);
					query.setQuery(Query);
					query.updateReport();						
					
					buffer = query.getbuffer();
					Querybuffer = query.getQuerybuffer();		
					value = query.getvalue();
					tid= query.gettid();
					tablename = query.gettablename();
					Filename = query.getReportname();
					Query = query.getQuery();
				}
			}
			catch (Exception e) 
			{
				e.printStackTrace();
			}
		}
	}


	public String getKey() 
	{
		return Key;
	}


	public void settypeofreport(String typeofreport) 
	{
		this.typeofreport =typeofreport;
	}
	public void setQueryMap(Hashtable<Object, Object> QueryMap) 
	{
		this.QueryMap.putAll(QueryMap);
	}
	public void setReportmap(Hashtable<Object, Object> ReportMap) 
	{
		this.Reportmap.putAll( ReportMap);
	}
	public void setreporton(String reporton) 
	{
		this.reporton = reporton; 
	}
	public void setViewObject(String value, char ch) 
	{	
		Hashtable<Object,Object> ReportMap =new Hashtable<Object,Object>();
		try 
		{	
			ResultSet rs = database.getSelect("SELECT * FROM command where msgtype='report_master' and process='view'  and message='query' order by order1");
			int indexofnextspchar=1;
			while(rs.next() && value.length()!=0)
			{
				String spchar =rs.getString("spchar"); 
				String Fieldname =rs.getString("fieldname");
				if(value.indexOf(spchar)!=-1)
				{
					indexofnextspchar = Index.indexOf(value, spchar, 1);
					if(indexofnextspchar==-1)
							indexofnextspchar = value.length();
					String newvalue="";
					if(!rs.isLast())
						 newvalue = value.substring(1, indexofnextspchar);
					else
						newvalue =value.substring(Fieldname.length()+1);
					String MasteridofField = gid.getId(Fieldname);
					if(!MasteridofField.equals("0"))
						Fieldname = MasteridofField;
					ReportMap.put(Fieldname, newvalue);
					value = value.substring(indexofnextspchar);
				}
				else
				{
					ReportMap.put(Fieldname, value);
					value ="";
				}
			}
			//if(table.isEmpty())
				//table.put(Key, value);
			//ReportMap.put(Key,table);
				
		} catch (Exception e) 
		{
			e.printStackTrace();
		}

		Set s1 = ReportMap.keySet();
		Iterator<Object> reportiterator= s1.iterator();
		Queryinterface query;
		String 
/*
*Pool Public Licence version 0.1 or later
*Released on 20th April 2008.
*Any voilations can be repoted at paritosh@wikiocean.net
*What is treated as voilations can be found at www.wikiocean.net/ppl1-voilations* 
******************************************************************************
* The contents of this file are subject Affero GPL v3 and later with additional conditions.
* The additional conditions are to be considered as part of the Affero GPL v3 and later.
* The additional conditions will supersede the Affero GPLv3 and later in case of conflict.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the License at http://www.gnu.org/licenses/agpl.html
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya.
* created by Paritosh Pungaliya are Copyright (C) Paritosh Pungaliya; All Rights Reserved.
* Contributions are Copyright (C) 2001-2008 Paritosh Pungaliya.

* Objective of the additional terms (license) 
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencie(s).
* 3) To avoid a situation where big corporates or investors can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the openness changes in companies that get Venture Capital funding.)
* (if you agree to this logic I invite you to our group to work together)

* Additional conditions
*  0) Source code will also include
* 	a) data dictionaries, data ontologies which are used to alter the behavior of code or to control           the features of application.
* 	b) Any code generated or derived automatically or manually from this code or logic
*  1) The revenue generated by deploying, implementing , selling services and other activities
*  	 based on this source code should be shared 100%, between the people working on it.
*  2) Capital invested should be given only fixed rate of return or linked to revenue/surplus growth for limited time.
*  3) People working on the project should always have an option to replace the capital provider in full or part with lower cost capital.
*  4) Their should not be any ownership which can be sold as a claim on future profit to be generated from the work of people working on the code.
*  5) Brand thus generated should belong to the group.
*  6) For old and new members revenue sharing should be equal for same work equivalent.
*  7) The share of revenue should be decided by bidding for work before allocation of work within the group.
*  8) Before bidding various method of measuring the work should be clearly defined and published in public domain. Any change in process should also be publised in public domain imediately.
*  9) All data related to to revenue sharing should be published as and when generated in public domain.
*  10) For group of people having size more than 10 people will have to make it open online for others to participate in bids. 
*  11) All people involved in related and sub project will be treated as a group.
*  12) Once this license is being used for a certain code then all and any other software services being provided by the group should complusorilly come under 100% revenue sharing.
*  13) Cross subsidization should not happen under this revenue share, no additional benefit should be drawn from the people, by the employing agency.
*  14) Any position or knowledge or legal structure created relating to people working on code, which adversely affects the spirit of 100% revenue share will be treated as a violation.
*  15) Revenue sharing defined.
*     a) includes direct revenue and also intangible gains like brand, and organization.
*     b) No personal judgment should be involved in judging the distribution of revenue.It has to be pre-defined formula based.
*     c)100% of revenue should be shared before expenses and recievers should have control over what expenses to incur.
*     d)For the work done in any month by contributing group member, working on any project can be given revenue share from the work done by the group, for a maximum of 18 months after that month.
*     e)Revenue should be apportioned to the month in which work is done and not when received.
*  The group fulfilling the above conditions also has to be a wiki ecosystem further defined in www.wekosystem.org or read about Wekosystem at www.wikiocean.net

*  ******************************************************************************
*/  
packagename ="combinereport.query.";
		char spchar=',';
		while(reportiterator.hasNext()) 
		{
			try
			{
				String Classname = reportiterator.next().toString();
				value = ReportMap.get(Classname).toString();
				String Querybuffer[]={};
				Classname= gid.getItem(Classname);
				Class c =Class.forName(
/*
*Pool Public Licence version 0.1 or later
*Released on 20th April 2008.
*Any voilations can be repoted at paritosh@wikiocean.net
*What is treated as voilations can be found at www.wikiocean.net/ppl1-voilations* 
******************************************************************************
* The contents of this file are subject Affero GPL v3 and later with additional conditions.
* The additional conditions are to be considered as part of the Affero GPL v3 and later.
* The additional conditions will supersede the Affero GPLv3 and later in case of conflict.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the License at http://www.gnu.org/licenses/agpl.html
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya.
* created by Paritosh Pungaliya are Copyright (C) Paritosh Pungaliya; All Rights Reserved.
* Contributions are Copyright (C) 2001-2008 Paritosh Pungaliya.

* Objective of the additional terms (license) 
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencie(s).
* 3) To avoid a situation where big corporates or investors can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the openness changes in companies that get Venture Capital funding.)
* (if you agree to this logic I invite you to our group to work together)

* Additional conditions
*  0) Source code will also include
* 	a) data dictionaries, data ontologies which are used to alter the behavior of code or to control           the features of application.
* 	b) Any code generated or derived automatically or manually from this code or logic
*  1) The revenue generated by deploying, implementing , selling services and other activities
*  	 based on this source code should be shared 100%, between the people working on it.
*  2) Capital invested should be given only fixed rate of return or linked to revenue/surplus growth for limited time.
*  3) People working on the project should always have an option to replace the capital provider in full or part with lower cost capital.
*  4) Their should not be any ownership which can be sold as a claim on future profit to be generated from the work of people working on the code.
*  5) Brand thus generated should belong to the group.
*  6) For old and new members revenue sharing should be equal for same work equivalent.
*  7) The share of revenue should be decided by bidding for work before allocation of work within the group.
*  8) Before bidding various method of measuring the work should be clearly defined and published in public domain. Any change in process should also be publised in public domain imediately.
*  9) All data related to to revenue sharing should be published as and when generated in public domain.
*  10) For group of people having size more than 10 people will have to make it open online for others to participate in bids. 
*  11) All people involved in related and sub project will be treated as a group.
*  12) Once this license is being used for a certain code then all and any other software services being provided by the group should complusorilly come under 100% revenue sharing.
*  13) Cross subsidization should not happen under this revenue share, no additional benefit should be drawn from the people, by the employing agency.
*  14) Any position or knowledge or legal structure created relating to people working on code, which adversely affects the spirit of 100% revenue share will be treated as a violation.
*  15) Revenue sharing defined.
*     a) includes direct revenue and also intangible gains like brand, and organization.
*     b) No personal judgment should be involved in judging the distribution of revenue.It has to be pre-defined formula based.
*     c)100% of revenue should be shared before expenses and recievers should have control over what expenses to incur.
*     d)For the work done in any month by contributing group member, working on any project can be given revenue share from the work done by the group, for a maximum of 18 months after that month.
*     e)Revenue should be apportioned to the month in which work is done and not when received.
*  The group fulfilling the above conditions also has to be a wiki ecosystem further defined in www.wekosystem.org or read about Wekosystem at www.wikiocean.net

*  ******************************************************************************
*/  
packagename+CapitalChar.makeFirstCharCapital(Classname));
				query = (Queryinterface) c.newInstance();
				query.setDbmanager(database);
				ResultSet rs = database.getSelect("SELECT * FROM command where msgtype='report_master' and message='"+Classname +"' and process='view' order by order1");
				ch='0';
				if(rs.next())
				{
					ch = rs.getString("spchar").toCharArray()[0];
				}	
				query.setViewObject(value.toString().replace("\"","").substring(Classname.length()+1),ch);
				//query.setQueryMap((Hashtable<Object, Object>)ReportMap.get(Classname));
				query.settablename("report_master");
				query.setbuffer(this.Querybuffer);
			   // query.initializeData();
				query.addReport();
				this.Querybuffer =query.getbuffer();			
				
			} 
			catch (ClassNotFoundException e) 
			{
				e.printStackTrace();
			} 
			catch (InstantiationException e)
			{
				e.printStackTrace();
			} 
			catch (Exception e) 
			{
				e.printStackTrace();
			}
		}
		    
	}
	public void setViewObject(Vector<Object> PropertyMap) 
	{	
		
	}
	
	public void setQuery(String Query)
	{
		this.Query = Query;
	}
	public String getQuery() 
	{
		return Query;
	}
	
	public int getcount() 
	{	
		return Querycount;
	}
	
	public void setCount(int count) 
	{
		this.Querycount=count+1;
	}
	public void setMaxcount(int count)
	{
		if(QueryMaxcount<Querycount)
			this.QueryMaxcount = Querycount;
	}
	public void buildQuery() 
	{
		for(int i=0;i<table.size();i++)
		{
			try 
			{
				String classname = table.get(Integer.toString(i+1)).toString();
				System.out.println("Class Name is "+classname);
				classname = gid.getId(classname).toLowerCase();
				if(QueryMap.containsKey(classname))
				{	
					query = (Queryinterface) QueryMap.get(classname);
					query.setQuerybuffer(Querybuffer);
					query.settablename(tablename);
					query.setReportmap(Reportmap);
					query.settypeofreport(typeofreport);
					query.setreporton(reporton);
				
					query.setValue(value);
					
					query.setQuery(Query);
					query.initializeData();		
					query.buildQuery();
					Query = query.getQuery();
					Querycount=query.getcount();				
					Querybuffer = query.getQuerybuffer();		
					value = query.getvalue();
					tid= query.gettid();
					tablename = query.gettablename();
					Filename = query.getReportname();
				}
			}
			catch (Exception e) 
			{
				e.printStackTrace();
			}
		}
	
	}
	
}
